package com.ddyx.service.impl.user;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.Store;
import com.ddyx.entity.StoreTemplate;
import com.ddyx.entity.user.UEmploye;
import com.ddyx.entity.user.UStore;
import com.ddyx.mapper.StoreTemplateMapper;
import com.ddyx.mapper.user.UEmployeMapper;
import com.ddyx.mapper.user.UStoreMapper;
import com.ddyx.service.user.UStoreService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.NoUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
@Slf4j
public class UStoreServiceImpl implements UStoreService{
	
	@Resource
	UStoreMapper uStoreMapper;
	
	@Resource
	UEmployeMapper uEmployeMapper;
	
	@Resource
	StoreTemplateMapper stmapper;
	
	@Transactional
	@Override
	public ResponseForm addStore(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			//邮费
			String goodsPrice = (String) paramMap.get("postage");
			BigDecimal postage = new BigDecimal(goodsPrice);
			paramMap.put("postage", postage);
			
			UStore bean  = new UStore();
			MyBeanUtils.populate(bean, paramMap);
			try {
				// 门店号设置S00000001
				PageHelper.startPage(1, 1);
				Example example = new Example(UStore.class);
				example.setOrderByClause("store_no DESC");
				List<UStore> stores= uStoreMapper.selectByExample(example);
				System.out.println(stores);
				String storeNo = null;
				//当size为0时,创建第一家门店,设置storeNo为S00000001
				if(stores.size()==0) {
					storeNo = "S00000001";
				}else{
					storeNo = stores.get(0).getStoreNo();
					storeNo = NoUtil.storeNoUil(storeNo);
				}
				//店长账号新增
				if(paramMap.get("mobile")==null&&paramMap.get("employeName")==null&&paramMap.get("employeName")==null) {
					result.setStatus(false);
					result.setMessage("必要的信息不能为空!");
					return result;
				}
				
				bean.setStoreNo(storeNo);
				bean.setCreateTime(new Date());
				uStoreMapper.insertSelective(bean);
				
				
				UEmploye ue = new UEmploye();
				MyBeanUtils.populate(ue, paramMap);
				ue.setStoreNo(bean.getStoreNo());
				ue.setRoleId(1);
				uEmployeMapper.insertSelective(ue);
				ue.setEmployeNo(String.valueOf(ue.getEmployeId()));
				uEmployeMapper.updateByPrimaryKeySelective(ue);
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Transactional
	@Override
	public ResponseForm editStore(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			//邮费
			String goodsPrice = (String) paramMap.get("postage");
			BigDecimal postage = new BigDecimal(goodsPrice);
			paramMap.put("postage", postage);
			
			UStore bean  = new UStore();
			MyBeanUtils.populate(bean, paramMap);
			if(bean.getCompanyId()!=null||bean.getBrandId()!=null) {
				result.setStatus(false);
				result.setMessage("the filed is not able to edit !");
				return result;
			}
			try {
				Example example = new Example(UStore.class);
				Criteria criteria = example.createCriteria();
				criteria.andEqualTo("storeNo", bean.getStoreNo());
				System.out.println("asd");
				uStoreMapper.updateByExampleSelective(bean, example);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getStoreList(RequestForm param) {
		ResponseForm result = new ResponseForm();

		try {
			if (param.getData() != null) {
				Map paramMap = (Map) param.getData();
				Integer employeId = (Integer) paramMap.get("employeId");
				UEmploye ue = new UEmploye();
				ue.setEmployeId(employeId);
				ue = uEmployeMapper.selectOne(ue);
				if (ue == null) {
					result.setStatus(false);
					result.setMessage("The user is not exists!");
					return result;
				}
				if (!ue.getRoleId().equals(3)) {
					result.setStatus(false);
					result.setMessage("You are not an administrator!");
					return result;
				}
				paramMap = com.ddyx.util.Page.parsePage(paramMap);
				Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
				Example example = new Example(UStore.class);
				example.setOrderByClause("create_time DESC");
				List<UStore> storeList = uStoreMapper.selectByExample(example);
				result.setData(storeList);
				result.setTotal((int) pageHelper.getTotal());
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
	@Override
	public ResponseForm getStoreInfo(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			String storeNo = (String)paramMap.get("storeNo");
			UStore store = new UStore();
			store.setStoreNo(storeNo);
			try {
				if(!StringUtils.isNotBlank(storeNo)){
					result.setStatus(false);
					result.setMessage("门店编号有误");
					return result;
				}
				store = uStoreMapper.selectOne(store);
				result.setData(store);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm storeList(RequestForm param) {
		ResponseForm result = new ResponseForm();

		try {
			if (param.getData() != null) {
				Example example = new Example(UStore.class);
				example.setOrderByClause("create_time DESC");
				List<UStore> storeList = uStoreMapper.selectByExample(example);
				result.setData(storeList);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}
	
	
	/*
	 * 专门用于获取门店信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getStoreDetails(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			Map<String, Object> paramMap = (Map<String, Object>) param.getData();
			Map map = uStoreMapper.getStoreDetails(paramMap);
			result.setData(map);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}


}
